package org.sun.sunshine.hp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.sun.sunshine.common.BusinessException;
import org.sun.sunshine.common.Resp;
import org.sun.sunshine.hp.entity.Happy;
import org.sun.sunshine.hp.service.HappyService;
import org.sun.sunshine.utils.WordUtil;
import org.sun.sunshine.websocket.EchoChannel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/happy")
public class HappyController {

    private final HappyService happyService;
    public HappyController(HappyService happyService) {
        this.happyService = happyService;
    }

    @GetMapping("send/{userId}")
    public Resp sendAllSocket(@PathVariable("userId") String userId){
        EchoChannel.sendMsg(userId,"11111");
        return Resp.success("success");
    }

    //统一异常处理，和自定义校验valid
    @PutMapping("/add")
    public Resp addHappy(@RequestBody @Validated Happy happy){
        if(!happyService.saveIfNotExist(happy)){
            throw new BusinessException(-1,"名称已存在");
        }
        return Resp.success("success");
    }

    @RequestMapping(value = "/page",method = {RequestMethod.POST})
    public IPage<Happy> page(@RequestBody Happy happy){
        return happyService.page(happy);
    }

    //缓存，缓存击穿，穿透，雪崩，布隆过滤器,限流

    /**
     * sync = true  解决缓存击穿（热点数据过期，大量请求同时打到数据库）
     * 配置中cache.redis.cache-null-values=true  解决缓存穿透（缓存空值）
     * 缓存雪崩，spring-cache 虽然提供了过期时间配置，但没有完整的解决方案需要自己写。
     *
     *  cacheable 中的value参数代表缓存的名称。
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @Cacheable(value="happy",sync = true)
    public Happy get(@PathVariable("id") Integer id){
        return happyService.getById(id);
    }

    @DeleteMapping("/delete/{id}")
    @CacheEvict("happy")
    public boolean delete(Integer id){
        return happyService.removeById(id);
    }

    @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Map<String,Object>> docMap=new HashMap<String, Map<String,Object>>();

        Map<String,Object> doc=new HashMap<>();
        doc.put("title","水稻参数采集");
        List<Map<String,Object>> items=new ArrayList<>();
        Map<String,Object> item=new HashMap<>();
        item.put("paramName","千粒重");
        item.put("qlz1",11);
        item.put("qlz2",12);
        item.put("qlz3",13);
        items.add(item);


        Map<String,Object> item1=new HashMap<>();
        item1.put("paramName","叶龄");
        item1.put("qlz1",11);
        item1.put("qlz2",12);
        item1.put("qlz3",13);
        items.add(item1);

        doc.put("items",items);
        docMap.put("doc",doc);

        WordUtil.exportWordByFreemarker(request,response,docMap,"freemaker.ftl","freemaker.docx");
    }


}